package lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class LockSupportTest {


    private char[] a1 = {'1','2','3','4','5','6'};
    private char[] a2 = {'A','B','C','D','E','F'};
    private ReentrantLock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
//    private Condition c2 = lock.newCondition();
//    private Condition c3 = lock.newCondition();



    public void m1(){
        int count = 0;
        lock.lock();
        while(count <= 4){
            System.out.print(a2[count++]);
            try {
                c1.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        c1.signal();
        lock.unlock();
    }

    public void m2(){
        int count = 0;
        lock.lock();
        while(count <= 4){
            System.out.print(a1[count++]);
            try {
                c1.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        c1.signal();
        lock.unlock();
    }
    public static void main(String[] args) {
        //要求用线程顺序打印A1B2C3....Z26
        LockSupportTest t = new LockSupportTest();
        new Thread(()->{
            t.m1();
        }).start();
        new Thread(()->{
            t.m2();
        }).start();
    }


}


